var wljsd = {
    setBtnStatus:function () {
        var row = $('#grid_wljsd').datagrid('getSelected');
        if (row && row.id && row.zt) {
            if (row.zt == 0 || row.zt == 1) {
                $("#btn_wljsd_mod").show();
                $("#btn_wljsd_del").show();
                $("#btn_wljsd_confirm").show();
            } else if (row.zt == 2 || row.zt == 3) {
                $("#btn_wljsd_mod").hide();
                $("#btn_wljsd_del").hide();
                $("#btn_wljsd_confirm").hide();
            }
        } else {
            $("#btn_wljsd_mod").hide();
            $("#btn_wljsd_del").hide();
            $("#btn_wljsd_confirm").hide();
        }
    },
    alwayShowDetail:function (rowData) {
        $('#layout_wljsd').layout('remove', 'center');
        $('#layout_wljsd').layout('add', {
            region:'center',
            split:true,
            href:$.webPath() + '/jsgl/wljsd/wljsd_detail?wljsdId=' + rowData.id
        });
    },
    showDetail:function (rowData) {
        if (rowData && $("#wljsd_detail_id").val() != rowData.id) {
            wljsd.alwayShowDetail(rowData);
        } else {
            $('#layout_wljsd').layout('remove', 'center');
            if (rowData) {
                $('#layout_wljsd').layout('add', {
                    region:'center',
                    split:true,
                    href:$.webPath() + '/jsgl/wljsd/wljsd_detail?wljsdId=' + rowData.id
                });
            }
        }
    }
}

$(function () {
    //表格初始加载
    $('#grid_wljsd').datagrid({
        url:$.webPath() + "/jsgl/wljsd/wljsdList",
        toolbar:"#toolbar_wljsd",
        fitColumns:true,
        singleSelect:true,
        pagination:true,
        pageNumber:1,
        fit:true,
        rownumbers:true,
        striped:true,
        columns:[
            [
                {field:'jsdh', title:'结算单号', align:'center', width:40, formatter:wljsCell},
                {field:'sqrq', title:'申请日期', align:'center', width:30},
                {field:'type', title:'类型', align:'center', width:15,
                    formatter:function (value) {
                        if (value == '0')
                            return '销售';
                        if (value == '1')
                            return '采购';
                        if (value == '2')
                            return '物流';
                    }
                },
                {field:'glqy_qc', title:'物流商', align:'center', width:30},
                {field:'glqy_qyfr_nsh', title:'纳税号', align:'center', width:35},
                {field:'fpze', title:'发票总金额(元)', align:'right', width:30,
                    formatter:function (value) {
                        var val = formatJeForHtml2(value);
                        return val;
                    }
                },
                {field:'fpzl', title:'发票种类', align:'center', width:18,
                    formatter:function (value) {
                        if (value == '0')
                            return '增票';
                        if (value == '1')
                            return '普票';
                        if (value == '2')
                            return '其他';
                        if (value == '3')
                            return '另开票';
                        if (value == '4')
                            return '物流发票';
                    }
                },
                {field:'hm', title:'发票号码', align:'center', width:35},
                {field:'sqr_xm', title:'申请人', align:'center', width:15},
                {field:'gs_jc', title:'公司', align:'center', width:30},
                {field:'fpthyy', title:'退回原因', align:'center', width:30},
                {field:'bz', title:'备注', align:'center', width:30},
                {field:"zt", title:"状态", width:15, align:'center',
                    formatter:function (value) {
                        if (value == '0')
                            return '未提交';
                        if (value == '1')
                            return '已退回';
                        if (value == '2')
                            return '已提交';
                        if (value == '3')
                            return '已审核';
                    }
                }
            ]
        ],
        onLoadSuccess:function () {
            $(this).datagrid('selectRow', 0);
            wljsd.setBtnStatus();
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onClickRow:function () {
            wljsd.setBtnStatus();
        },
        onSelect:function (rowIndex, rowData) {
            wljsd.setBtnStatus();
        }
    });
    //**********按钮触发事件**********
    //新增按钮
    $("#btn_wljsd_add").click(function () {
        wljsd_add('新增结算单', 'add_wljsd');
    });
    //修改按钮
    $("#btn_wljsd_mod").click(function () {
        wljsd_modify('修改结算单', 'update_wljsd');
    });
    //删除按钮
    $("#btn_wljsd_del").click(function () {
        wljsd_delete();
    });
    // 提交按钮
    $("#btn_wljsd_confirm").click(function () {
        wljsd_confirm();
    });
    //查询按钮
    $("#btn_wljsd_search").click(function () {
        wljsd_search();
    });
    //双击事件
    $("#grid_wljsd").datagrid({
        onDblClickCell:function (index, field) {
            if (field == "jsdh") {
                wljsd_modify('修改结算单', 'update_wljsd');
            }
        }
    });
    //加载查询条件中的申报公司下拉框
    $("#_gs").combotree({
        loadFilter:function (rows) {
            return convert(rows);
        }
    });
});

//新增物流单结算
function wljsd_add(title, action) {
    var href = $.webPath() + "/jsgl/wljsd/wljsd_content";
    var actionUrl = $.webPath() + "/jsgl/wljsd/add_wljsd";
    wljsd_showDialog(title, href, actionUrl);
}

//修改物流单结算
function wljsd_modify(title, action) {
    var record = $("#grid_wljsd").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录!", "warning");
        return;
    }
    var href = $.webPath() + "/jsgl/wljsd/wljsd_content?wljsdId=" + record.id;
    var actionUrl = $.webPath() + "/jsgl/wljsd/" + action;
    wljsd_showDialog(title, href, actionUrl);
}

//弹出新增或修改的窗口
function wljsd_showDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        iconCls:'icon-add2',
        width:1024,
        height:450,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'保存',
                handler:function () {
                    $(win).mask();
                    $('#form_wljsd').form('submit', {
                        url:actionUrl,
                        onSubmit:function (param) {
                            //结束编辑状态
                            $('#grid_wld').edatagrid("saveRow");
                            var r = $('#grid_wld').edatagrid("getSelected");
                            var index = $('#grid_wld').edatagrid("getRowIndex", r);
                            $('#grid_wld').edatagrid("endEdit", index);
                            $('#grid_wld').datagrid('acceptChanges');
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            var wldhm = $("#wljsd_hm").val();
                            if (!wldhm || wldhm == '') {
                                $(win).mask('hide');
                                $.messager.alert("提示", "请填写发票号码!", "waring");
                                $("#form_wljsd .tabs-title :eq(0)").click();
                                return false;
                            }
                            var data = $('#grid_wld').edatagrid("getData");
                            var zjbcdata = $('#grid_zjbcd_wljsd').edatagrid("getData");
                            if (!data.rows.length) {
                                $(win).mask('hide');
                                $.messager.alert("提示", "请选择物流单!", "waring");
                                $("#form_wljsd .tabs-title :eq(1)").click();
                                return false;
                            }
                            var arrayPrefix = 'wljsd.wldList';
                            var zjbcPrefix = 'wljsd.zjbcdList';
                            var paramString = "{";
                            for (var i = 0; i < data.rows.length; i++) {
                                if (data.rows[i].me == null || data.rows[i].me == 0) {
                                    $(win).mask('hide');
                                    $('#form_wljsd').tabs('select', "结算单明细");
                                    $.messager.alert('提示', '第' + (i + 1) + '行的总面额不能为0 !', 'info');
                                    return false;
                                }
                                var objectString = $.toJSONStringPrefix(data.rows[i], arrayPrefix + "[" + i + "].");
                                paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                            }

                            for (var i = 0; i < zjbcdata.rows.length; i++) {
                                {
                                    var objectString = $.toJSONStringSubPrefix(zjbcdata.rows[i], zjbcPrefix + "[" + i + "].", [
                                        'lb.id',
                                        'je',
                                        'cgdmx.id',
                                        'cgd.id'
                                    ]);
                                    paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                                }
                            }

                            if (data.rows && data.rows.length > 0)
                                paramString = paramString.substring(0, paramString.length - 1);
                            paramString += "}";
                            $.extend(param, $.parseJSON(paramString));
                            return isValid;
                        },
                        success:function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_wljsd').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//删除物流结算单
function wljsd_delete() {
    var row = $('#grid_wljsd').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否删除该物流单结算？', function (r) {
            if (r) {
                var index = $('#grid_wljsd').datagrid('getRowIndex', row);
                $.ajax({
                    url:$.webPath() + "/jsgl/wljsd/delete_wljsd",
                    dataType:'json',
                    type:'POST',
                    data:{wljsdId:row.id},
                    beforeSend:function () {
                        $("#layout_wljsd").mask();
                    },
                    success:function (data) {
                        $("#layout_wljsd").mask("hide");
                        if (data.statusCode == 200) {
                            $('#grid_wljsd').datagrid('deleteRow', index);
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        } else {
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要删除的物流单结算！", "warning");
    }
}

//物流结算单确认
function wljsd_confirm() {
    var row = $('#grid_wljsd').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否对该物流单结单进行确认？', function (r) {
            if (r) {
                $.ajax({
                    url:$.webPath() + "/jsgl/wljsd/change_status",
                    dataType:'json',
                    type:'POST',
                    data:{wljsdId:row.id, zt:2},
                    beforeSend:function () {
                        $("#layout_wljsd").mask();
                    },
                    success:function (data) {
                        $("#layout_wljsd").mask("hide");
                        if (data.statusCode == 200) {
                            $('#grid_wljsd').datagrid('reload');
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        } else {
                            $('#grid_wljsd').datagrid('reload');
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要确认的物流单结算！", "warning");
    }
}

//查询物流单结算
function wljsd_search() {
    var jsdh = $('#wljsdJsdh').val();
    var glqyId = $('#wljsdGlqy').combotree('getValue') == null ? null : $('#wljsdGlqy').combotree('getValue');
    var glqyQc = $('#wljsdGlqy').combotree('getText') == null ? null : $('#wljsdGlqy').combotree('getText');
    var gsId = $('#wljsdGs').combotree('getValue') == null ? null : $('#wljsdGs').combotree('getValue');
    var gsJc = $('#wljsdGs').combotree('getText') == null ? null : $('#wljsdGs').combotree('getText');
    var fpzlId = $('#wljsdFpzl').combobox('getValue') == null ? null : $('#wljsdFpzl').combobox('getValue');
    var sqrqStart = $('#wljsdSqrq_start').datebox('getValue');
    var sqrqEnd = $('#wljsdSqrq_end').datebox('getValue');
    var zt = $('#wljsdZt').combobox('getValue') == null ? null : $('#wljsdZt').combobox('getValue');
    $('#grid_wljsd').datagrid('load',
        {
            jsdh:jsdh,
            glqyId:glqyId,
            glqyQc:glqyQc,
            gsId:gsId,
            gsJc:gsJc,
            fpzlId:fpzlId,
            sqrqStart:sqrqStart,
            sqrqEnd:sqrqEnd,
            zt:zt
        }
    );
}
